VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' global vars
Dim MyAddin_ As RoseRT.AddIn
Dim MyMenuItems_(3) As RoseRT.ContextMenuItem
Dim mainForm As New FormX3DUML
        
' context menu internal names used to identify individual menu items
Const CMID_COMMAND1 As String = "command1"
Const CMID_COMMAND2 As String = "command2"
Const CMID_COMMAND3 As String = "command3"

Sub OnActivate(pRoseApp As RoseRT.Application)
    Dim aContextMenu As RoseRT.ContextMenuItem
    Dim AddIns As RoseRT.AddInCollection
    Set AddIns = pRoseApp.AddInManager.AddIns
    
    ' find the add-in
    Set MyAddin_ = AddIns.GetFirst("x3duml")
    
    If MyAddin_ Is Nothing Then
        MsgBox ("Error cannot add context menus")
    Exit Sub
    End If
    
    ' Build the context menu
    Set aContextMenu = MyAddin_.AddContextMenuItemForClass("Diagram", "Submenu X3D-UML", "")
        Set MyMenuItems_(1) = MyAddin_.AddContextMenuItemForClass("Diagram", "3D State Machine Diagram", CMID_COMMAND1)
        Set MyMenuItems_(2) = MyAddin_.AddContextMenuItemForClass("Diagram", "Report Issue", CMID_COMMAND2)
        Set MyMenuItems_(3) = MyAddin_.AddContextMenuItemForClass("Diagram", "Check for Updates", CMID_COMMAND3)
    Set aContextMenu = MyAddin_.AddContextMenuItemForClass("Diagram", "EndSubmenu", "")
End Sub

Function OnEnableContextMenuItemsForClass(pRoseApp As RoseRT.Application, items As RoseRT.ControllableElementCollection) As Boolean

    Dim cmItem As RoseRT.ContextMenuItem
    Dim cmCollection As RoseRT.ContextMenuItemCollection
    
    ' activate this menu item only if two or more model elements are selected
    'If items.Count > 1 Then
    'MyMenuItems_(1).MenuState = RoseRT.rsEnabled
    'OnEnableContextMenuItemsForObjects = True
    'Else
    MyMenuItems_(1).MenuState = RoseRT.rsDisabled
    OnEnableContextMenuItemsForObjects = False
    'End If
End Function

Function OnSelectedContextMenuItemForObjects(pRoseApp As RoseRT.Application, InternalName As String, items As RoseRT.ControllableElementCollection) As Boolean
    Dim theStateDiagramCollection As RoseRT.StateDiagramCollection
    Dim theStateDiagram As RoseRT.StateDiagram
    Dim theCapsules As RoseRT.CapsuleCollection
    Dim theCapsule As RoseRT.Capsule
    Dim myElement As ControllableElement
    Dim theTopState As RoseRT.CompositeState
    Dim theStateMachine As RoseRT.StateMachine
    Dim theStateMachineDiagram As RoseRT.StateDiagram
    
    Dim msg As String
    msg = InternalName + ": "
    Dim i As Integer
    'Dim myElement As RoseRT.ControllableElement
    For i = 1 To items.Count Step 1

    Set myElement = items.GetAt(i)
    Set theStateDiagram = myElement
    Set theCapsule = theStateDiagram.ParentModelElement
    Set theStateMachine = theCapsule.StateMachine
    Set theStateMachineDiagram = theStateMachine.Diagram
    Set theTopState = theStateMachine.Top
    
     'MsgBox ("Root." + theTopState.Name + ".")
    
    Call RecurseStateDiagram("Root." + theTopState.Name + ".", theStateMachineDiagram.ViewElements)
    'msg = msg + myElement.Name + " " + myElement.GetUniqueID() + " " + theStateDiagram.ParentModelElement.Name + " " + theStateDiagram.ParentModelElement.IdentifyClass
    
    Next
    'MsgBox (msg)
    ' create a form object
    
    
        'code to adapt from VB 2008
'        theCapsules = theModel.GetAllCapsules()
'        theCapsule = theCapsules.GetAt(theCapsules.FindFirst("TrafficLightAustrian"))
'        Me.Text = Me.Text + " - " + theCapsule.Name
        ' theStateMachine = myElement.StateMachine
        'theStateMachineDiagram = theStateMachine.Diagram
        'theTopState = theStateMachine.Top
'        RecurseStateDiagram("Root." + theTopState.Name + ".", theStateMachineDiagram.ViewElements)
    mainForm.Caption = mainForm.Caption + " " + theStateDiagram.ParentModelElement.Name
    
    
    
    ' display the form
    'Call RecurseStateDiagram("top", theStateDiagram.ViewElements)
    'Call RecurseStateDiagram("Root." + theTopState.Name + ".", theStateDiagram.ViewElements)
    'Call mainForm.X3DUMLv21.AddTextView(msg, 0, 0, 0)
    'Call mainForm.X3DUMLv21.ShowSplashScreen
    Call mainForm.X3DUMLv21.RenderDiagram
    ' mainForm.Caption = mainForm.Caption + "Root." + theTopState.Name + "."
    
    mainForm.Show vbModal
    OnSelectedContextMenuItemForObjects = True
End Function

Private Function StringKludge() As String
    Dim Res As String
    Res = "String"
    StringKludge = Res
End Function


Public Sub RecurseStateDiagram(ByVal fullyQualifiedName As String, ByVal theViewElements As RoseRT.ViewElementCollection)

        Call mainForm.X3DUMLv21.SetOrCreateCurrentDiagram(fullyQualifiedName)
        Dim theViewElement As RoseRT.ViewElement
        Dim theCompositeStateView As RoseRT.CompositeStateView
        Dim i As Integer
        For i = 1 To theViewElements.Count
            Set theViewElement = theViewElements.GetAt(i)
            Select Case theViewElement.IdentifyClass
                Case "StatePerimeterView"
                    Call mainForm.X3DUMLv21.AddStateView(theViewElement.Width, _
                                                theViewElement.Height, _
                                                theViewElement.XPosition - theViewElement.Width / 2, _
                                                theViewElement.YPosition - theViewElement.Height / 2, _
                                                2)
                Case "CompositeStateView"
                    aString = mainForm.X3DUMLv21.AddTextView(theViewElement.Name, _
                                                theViewElement.XPosition, _
                                                theViewElement.YPosition, _
                                                -2)
                    Call mainForm.X3DUMLv21.AddStateView(theViewElement.Width, _
                                                theViewElement.Height, _
                                                theViewElement.XPosition - theViewElement.Width / 2, _
                                                theViewElement.YPosition - theViewElement.Height / 2, _
                                                0)
                    Set theCompositeStateView = theViewElement
                    If Not theCompositeStateView.SubDiagram Is Nothing Then
                        Call RecurseStateDiagram(fullyQualifiedName + theCompositeStateView.Name + ".", theCompositeStateView.SubDiagram.ViewElements)
                        Call mainForm.X3DUMLv21.LinkElementToDiagram(aString, fullyQualifiedName + theCompositeStateView.Name + ".")
                        Call mainForm.X3DUMLv21.SetCurrentDiagram(fullyQualifiedName)
                    End If
                Case "InitialPointView"
                    Call mainForm.X3DUMLv21.AddPseudoStateView(theViewElement.Width, _
                                                    theViewElement.Height, _
                                                    theViewElement.XPosition, _
                                                    theViewElement.YPosition, _
                                                    0, _
                                                    "Initial")
                Case "JunctionPointView"
                    Call mainForm.X3DUMLv21.AddPseudoStateView(theViewElement.Width, _
                                                    theViewElement.Height, _
                                                    theViewElement.XPosition, _
                                                    theViewElement.YPosition, _
                                                    0, _
                                                    "Junction")
                Case "JunctionAdornmentView"
                    Call mainForm.X3DUMLv21.AddPseudoStateView(theViewElement.Width, _
                                                    theViewElement.Height, _
                                                    theViewElement.XPosition, _
                                                    theViewElement.YPosition, _
                                                    0, _
                                                    "History")
                    'Case "BranchPointView"
                    'Case "ChoicePointView"
                Case "NoteView"
                    Dim theNoteView As RoseRT.NoteView
                    Set theNoteView = theViewElement
                    Call mainForm.X3DUMLv21.AddTextView(theNoteView.Text, theViewElement.XPosition, theViewElement.YPosition, -1)
                Case "ViewElement"
'                    Dim theString As String
'                    Set theString = theViewElement.GetPropertyClassName()
                    'If theString Then
                    If Not (StrComp(theViewElement.GetPropertyClassName(), "StateTransition")) Then
                        Dim theLineVertexCollection As RoseRT.LineVertexCollection
                        Set theLineVertexCollection = theViewElement.LineVertices
                        Dim theLineWayPoint As RoseRT.LineVertex
                        Dim theWayPointCSVString As String
                        theWayPointCSVString = "" 'format for string MUST be "x,y,z,x,y,z,"....
                        Dim w As Integer
                        For w = 1 To theLineVertexCollection.Count
                            Set theLineWayPoint = theLineVertexCollection.GetAt(w)
                            'theWayPointCSVString = theWayPointCSVString + theLineWayPoint.GetXPosition().ToString() + "," + theLineWayPoint.GetYPosition.ToString + ",0,"
                            theWayPointCSVString = theWayPointCSVString + Str(theLineWayPoint.GetXPosition) + "," + Str(theLineWayPoint.GetYPosition) + ",0,"
                        Next
                        'Log(theWayPointCSVString)
                        Call mainForm.X3DUMLv21.AddTransitionView(theWayPointCSVString)
                        'Call mainForm.X3DUMLv21.AddTextView("StateTransition", theViewElement.XPosition, theViewElement.YPosition, 0)
                    End If
                Case Else
                    'Create Text for anything left over
                    Call mainForm.X3DUMLv21.AddTextView(theViewElement.IdentifyClass, theViewElement.XPosition, theViewElement.YPosition, 0)
            End Select
        Next

End Sub


